package com.recruit.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.recruit.model.FavorDO;
import com.recruit.model.PositionDO;
import com.recruit.service.FavorService;
import com.recruit.vo.CreatedVO;
import com.recruit.vo.DeletedVO;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.Logger;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.core.annotation.PermissionModule;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.Positive;
import java.util.List;
import java.util.Objects;

/**
 * @author DHF
 */
@RestController
@RequestMapping("/recruit/favor")
@PermissionModule(value = "收藏职位")
@Api(tags = "职位收藏相关接口")
public class FavorController {

    @Resource
    private FavorService favorService;

    /**
     * 添加收藏
     *
     * @return
     */
    @Logger(template = "添加收藏")
    @PostMapping("")
    @GroupRequired
    @PermissionMeta(value = "添加收藏")
    @ApiOperation(value = "添加收藏")
    public CreatedVO create(
            @ApiParam(value = "收藏实体", required = true)
            @RequestBody FavorDO favorDO) {
        // 查询是否已经收藏过该职位
        FavorDO favor = find(favorDO.getUserId(), favorDO.getPositionId());
        if (Objects.nonNull(favor)) {
            // 已经收藏过该职位
            throw new NotFoundException(40010);
        }
        favorService.create(favorDO);
        // 未收藏过该职位，可以收藏
        return new CreatedVO(1600);
    }

    /**
     * 根据用户id和职位id取消收藏该职位
     *
     * @param userId
     * @param positionId
     * @return
     */
    @Logger(template = "取消收藏")
    @DeleteMapping("/cancel")
    @GroupRequired
    @PermissionMeta(value = "取消收藏")
    @ApiOperation(value = "取消收藏")
    public DeletedVO cancelFavor(
            @ApiParam(value = "userId", required = true)
            @RequestParam @Positive(message = "{id.positive}") Integer userId,
            @ApiParam(value = "职位Id", required = true)
            @RequestParam @Positive(message = "{id.positive}") Integer positionId) {
        favorService.cancelFavor(userId, positionId);
        return new DeletedVO(1500);
    }

    /**
     * 根据用户id查询该用户的所有收藏
     *
     * @param userId
     * @return
     */
    @GetMapping("/{userId}")
    @GroupRequired
    @PermissionMeta(value = "查看我的收藏")
    @ApiOperation(value = "查看我的收藏")
    public List<PositionDO> getAllByUserId(
            @ApiParam(value = "用户id", required = true)
            @PathVariable(value = "userId") @Positive(message = "{id.positive}") Integer userId) {
        return favorService.getAllByUserId(userId);
    }

    /**
     * 根据用户id和职位id查询收藏，避免重复收藏同一职位
     *
     * @param userId
     * @return
     */
    @GetMapping("/find")
    @ApiOperation(value = "根据用户id和职位id查询收藏，避免重复收藏同一职位")
    public FavorDO find(
            @ApiParam(value = "userId", required = true)
            @RequestParam Integer userId,
            @ApiParam(value = "职位id", required = true)
            @RequestParam Integer positionId) {
        QueryWrapper<FavorDO> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId).eq("position_id", positionId);
        return favorService.getOne(wrapper);
    }
}
